package com.wsz.eduorder.controlleer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsz.commonutils.JwtUtils;
import com.wsz.commonutils.R;
import com.wsz.eduorder.pojo.Order;
import com.wsz.eduorder.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/eduorder/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request) {
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        String orderNo = orderService.createOrder(courseId,memberIdByJwtToken);
        return R.ok().data("orderId",orderNo);
    }

    @GetMapping("/getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId){

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderId);

        Order order = orderService.getOne(queryWrapper);

        return R.ok().data("item",order);
    }

    @ApiOperation("根据课程Id和用户Id查询订单状态")
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,
                               @PathVariable String memberId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("status",1);
        int count = orderService.count(queryWrapper);

        if(count <= 0)
        return false;
        else return true;
    }


    @ApiOperation("根据用户Id查询课程Id")
    @GetMapping("/getBuyCourse/{id}")
    public R getBuyCourse(@PathVariable String id){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",id);
        queryWrapper.eq("status",1);
        queryWrapper.select("course_id");
        List<Order> list = orderService.list(queryWrapper);
        List<String> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Order order = list.get(i);
            list1.add(order.getCourseId());
        }
        return R.ok().data("list",list1);
    }

    @ApiOperation("查询所有订单列表并分页")
    @PostMapping("/getAllOrder/{current}/{limit}")
    public R getAllOrder(@PathVariable long current,
                         @PathVariable long limit){
        System.out.println(current+"    ===     "+limit);
        Page<Order> page = new Page<>(current,limit);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("id");
        IPage<Order> page1 = orderService.page(page, null);
        List<Order> records = page1.getRecords();
        long total = page1.getTotal();
        return R.ok().data("orders",records).data("total",total);
    }


    @ApiOperation("根据ID删除订单")
    @DeleteMapping("/{id}")
    public R deleteOrderById(@PathVariable String id){
        boolean b = orderService.removeById(id);
        if(b) return R.ok();
        else return R.error();
    }

}
